package com.alibaba.dubbo.remoting.interceptor;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.Interceptor;
import com.alibaba.dubbo.remoting.RemoteCalling;
import com.alibaba.dubbo.remoting.buffer.ChannelBufferInputStream;

/**
 * 代理
 * @author kingbo.ruan
 * 2014.09.16
 *
 */
public class InterceptorWrapper implements Interceptor {
	protected List<Interceptor> interceptors;
	
	
	public InterceptorWrapper(){
		this.interceptors = new ArrayList<Interceptor>();
	}
	
	public InterceptorWrapper(Interceptor interceptor){
		this.interceptors = new ArrayList<Interceptor>();
		if(interceptor != null){
			this.interceptors.add(interceptor);
		}
	}
	
	public List<Interceptor> getInterceptors() {
		return interceptors;
	}

	public void setInterceptors(List<Interceptor> interceptors) {
		this.interceptors = interceptors;
		if(this.interceptors == null){
			this.interceptors = new ArrayList<Interceptor>();
		}
	}

	public InterceptorWrapper add(Interceptor interceptor){
		if(interceptor != null){
			this.interceptors.add(interceptor);
		}
		return this;
	}
	

	@Override
	public boolean acceptAfterConnected(InetAddress remoteAddress) {
		if(!this.interceptors.isEmpty()){
			for(Interceptor interceptor : this.interceptors){
				if(!interceptor.acceptAfterConnected(remoteAddress)) return false;
			}
		}
		
		return true;
	}


	@Override
	public boolean acceptAfterDecodeHeader(InetAddress remoteAddress, Map<String, String> headers, int data) {
		if(!this.interceptors.isEmpty()){
			for(Interceptor interceptor : this.interceptors){
				if(!interceptor.acceptAfterDecodeHeader(remoteAddress, headers, data)) return false;
			}
		}
		return true;
	}
	
	@Override
	public boolean acceptAfterDecodeBody(InetAddress remoteAddress, Map<String, String> headers, RemoteCalling calling){
		if(!this.interceptors.isEmpty()){
			for(Interceptor interceptor : this.interceptors){
				if(!interceptor.acceptAfterDecodeBody(remoteAddress, headers, calling)) return false;
			}
		}
		return true;
	}


	@Override
	public boolean acceptAfterHead(InetAddress remoteAddress, int total, int body) {
		if(!this.interceptors.isEmpty()){
			for(Interceptor interceptor : this.interceptors){
				if(!interceptor.acceptAfterHead(remoteAddress, total, body)) return false;
			}
		}
		return true;
	}
	
	@Override
	public boolean acceptBeforeEncodeBody(InetAddress remoteAddress, Map<String, String> headers) {
		if(!this.interceptors.isEmpty()){
			for(Interceptor interceptor : this.interceptors){
				if(!interceptor.acceptBeforeEncodeBody(remoteAddress, headers)) return false;
			}
		}
		return true;
	}
	
	@Override
	public boolean acceptBeforeEncodeHeader(InetAddress remoteAddress, Map<String, String> headers) {
		if(!this.interceptors.isEmpty()){
			for(Interceptor interceptor : this.interceptors){
				if(!interceptor.acceptBeforeEncodeHeader(remoteAddress, headers)) return false;
			}
		}
		return true;
	}
}
